﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Module.Category.Properties;
using QLBH.DTO;
using QLBH.Base;
using QLBH.Model.Controller;
using QLBH.DataModel;

namespace Module.Category
{
     
    public partial class PositionDialog : Form
    {
        private eDialogType dialogType;

        public eDialogType DialogType
        {
            get { return dialogType; }
            set 
            { 
                dialogType = value;
                if (dialogType == eDialogType.DL_ADD)
                {
                    this.Text = "Thêm chức vụ";
                    this.Icon = Icon.FromHandle(Resources.add_24.GetHicon());
                }
                else
                {
                    this.Text = "Sửa thông tin chức vụ";
                    this.Icon = Icon.FromHandle(Resources.write_24.GetHicon());
                }
            }
        }
        private ChucVuDTO position;

        public ChucVuDTO Position
        {
            get { return position; }
            set { position = value; }
        }


        public PositionDialog()
        {
            InitializeComponent();
            position = new ChucVuDTO();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                DataInputHelper.CheckTextData(txtPositionName.Text, "Tên chức vụ");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtPositionName.SelectAll();
                txtPositionName.Focus();
                return;
            }
            // Add to database
            try
            {
                UpdateInformationfromControl();
                ChucVu newPos=PositionController.Instance.Add(position.ToEntity());
                position = newPos.ToDTO();
                DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }           
        }

        private void UpdateInformationfromControl()
        {
            position.TenCV = txtPositionName.Text;
            position.GhiChu = txtNote.Text;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
